.h файл:
class CVertex
{
public:
CVertex();
CVertex(float vx, float vy, float vz);
CVertex(const CVector& v);
virtual ~CVertex();
public:
float x, y, z;
CVertex& Displace(const CVector& v);
CVertex GetDisplaced(const CVector& v) const;
float DistanceTo(const CVertex& v) const;
CVertex& operator= (const CVertex& v);
bool operator==(const CVector& v) const;
bool operator!=(const CVector& v) const;
};
.cpp файл:
CVertex::CVertex(): x(0), y(0), z(0){};
CVertex::CVertex(float vx, float vy, float vz):
x(vx), y(vy), z(vz){};
CVertex::CVertex(const CVector& v): x(v.x), y(v.y), z(v.z){};
CVertex::~CVertex(){};
CVertex& CVertex::Displace(const CVector& v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
CVertex CVertex::GetDisplaced(const CVector& v) const
{
return CVertex(x+v.x, y+v.y, z+v.z);
}
float CVertex::DistanceTo(const CVertex& v) const
{
return (float)sqrt(
(v.x-x)*(v.x-x)+(v.y-y)*(v.y-y)+(v.z-z)*(v.z-z));
}
CVertex& CVertex::operator=(const CVertex& v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
bool CVertex::operator==(const CVector& v) const
{
if (x==v.x && y==v.y && z==v.z)
return true;
else
return false;
}
bool CVertex::operator!=(const CVector& v) const
{
return !(*this==v);
}
В класс CVector добавляем функции для создания вектора по одной или двум вершинам:
CVector::CVector(const CVertex& v): x(v.x), y(v.y), z(v.z){};
CVector::CVector(const CVertex& V1, const CVertex& V2):
x(V2.x-V1.x), y(V2.y-V1.y), z(V2.z-V1.z){};
Файл со всеми классами геометрических операций можно найти, например, в главе
шаблоны для OpenGL.